<?php
$my_yamii_lbl = BUtils::T('my_home_lbl');

$this->pageTitle = $my_yamii_lbl;

$points = Yii::app()->locale->numberFormatter->formatDecimal($row['points']);
$amount = Yii::app()->locale->numberFormatter->formatDecimal($row['amount']);

$head_photo_url = empty($row['picture']) ? Yii::app()->baseUrl . '/img/user-128.png' : $row['picture'];
?>
<div class="section-wrapper post-w">
<div class="container">
	<div class="row">
		<div class="span12">
			<ul class="nav nav-tabs">
				<li>
					<a data-toggle="tab" href="#tab-settings" data-id="tab-settings" data-url="<?php echo Yii::app()->createUrl('/user/settings'); ?>">
						<i class="icon-cog"></i> <?php echo BUtils::T('settings_lbl'); ?>
					</a>
				</li>
				<li>
					<a data-toggle="tab" href="#tab-password" data-id="tab-password" data-url="<?php echo Yii::app()->createUrl('/user/password'); ?>">
						<i class="icon-lock"></i> <?php echo Yii::t('app', 'Update password'); ?>
					</a>
				</li>
				<li>
					<a data-toggle="tab" href="#tab-notification" data-id="tab-notification" data-url="<?php echo Yii::app()->createUrl('/user/notification'); ?>">
						<i class="icon-bell"></i> <?php echo Yii::t('app', 'Notification settings'); ?>
					</a>
				</li>
			</ul>
			<div class="tab-content">
				<div class="tab-pane" id="tab-settings"></div>
				<div class="tab-pane" id="tab-password"></div>
				<div class="tab-pane" id="tab-notification"></div>
			</div>
		</div>
	</div>
</div>
</div>


<?php
$name = Yii::app()->user->getState('name');
if (empty($name)) { ?>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3 id="myModalLabel">Welcome to Yamii!</h3>
	</div>
	<div class="modal-body">
		<p>Enter your name</p>
		<?php echo CHtml::textField('user_name', '', array('id'=>'user_name', 'style'=>'width:90%;')) ?>
		<p class="help-block error-info hide">Please input your name!</p>
	</div>
	<div class="modal-footer">
		<button class="btn btn-primary" id="save-name"><?php echo Yii::t('app', 'Save'); ?></button>
	</div>
</div>
<?php } ?>

<script>
$(function() {
	//tab ajax loading
	var cache = [];
	$('a[data-toggle="tab"]').on('shown', function (e) {
		var eId = $(this).data('id'), url = $(this).data('url');
		var _panel = $('#'+eId);
		if (url !== '') {
			if (!cache[eId]) {
				$.ajax({
					url: url, data: {t:null}, type: 'POST',
					beforeSend: function() {
						_panel.html('<div class="text-center"><img src="<?php echo Yii::app()->baseUrl; ?>/img/loader.gif" /></div>');
					},
					error: function(jqXHR) {
						_panel.html(jqXHR.responseText);
					},
					success: function(html) {
						cache[eId] = html;
						_panel.html(html);
					}
				});
			}
		}
    });
	//show the first tab
	$('a[data-toggle="tab"]:first').tab('show');
	
	//if there is no user name, then popup a form to input user name
	$('#myModal').modal({keyboard:false,backdrop:'static'});
	$('#save-name').click(function(){
		$('.help-block.error-info').hide();
		var name = $('#user_name').val();
		if (name !== '') {
			$.post('<?php echo Yii::app()->createUrl('/user/update'); ?>', {name:name}, function(data){
				if (data.code === 200) {
					location.reload();
				} else {
					$('.help-block.error-info').show();
				}
			}, 'json');
		} else {
			$('.help-block.error-info').show();
		}
	});
	$('#user_name').focus(function(){
		$('.help-block.error-info').hide();
	});
});
</script>